/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.bazi2.controllers;

import com.asseco.bazi2.models.Klient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.asseco.mio.codex.controllers.*;
import org.springframework.validation.Validator;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleTypes;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Bojan
 */
@Controller
@RequestMapping("/listsifrarnik*")
public class ListSifrarnikController extends BaseFormController {

    @Autowired(required = false)
    Validator validator;

    @RequestMapping(method = RequestMethod.GET)
    public Klient showForm(HttpServletRequest request, HttpServletResponse response) {

        return new Klient();
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView onSubmit(Klient klient, BindingResult errors, HttpServletRequest request,
            HttpServletResponse response) throws SQLException {



        Configuration c = new Configuration().configure();
        SessionFactory sf = c.buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        Connection con = session.connection();  // obtain JDBC connection from Session object
        CallableStatement cs = con.prepareCall("{ call baza.NOV_SERVIS.prebaruvanje_klienti_kod(?,?,?,?) }");
        cs.setString(2, "%"+klient.getKod()+"%");
        cs.setString(3, "%"+klient.getIme()+"%");
        cs.setString(4, "%"+klient.getPrezime()+"%");

        List<Klient> resultList = new ArrayList<Klient>();


        cs.registerOutParameter(1, OracleTypes.CURSOR);
        cs.execute();
        ResultSet resultSet = (ResultSet) cs.getObject(1);
        if (resultSet != null) {
            while (resultSet.next()) {

                Klient tempKlient = new Klient();
                tempKlient.setId(Integer.parseInt(resultSet.getString(1)));
                tempKlient.setKod(resultSet.getString(2));
                tempKlient.setIme(resultSet.getString(3));
                tempKlient.setPrezime(resultSet.getString(4));
                resultList.add(tempKlient);
            }
            resultSet.close();
        }

        session.getTransaction().commit();
        session.close();
        sf.close();



        return new ModelAndView().addObject("resultList", resultList);
    }
}
